package com.hobart.learning.lagou.edu.dao.impl;

import com.hobart.learning.lagou.edu.annotation.Autowired;
import com.hobart.learning.lagou.edu.annotation.Repository;
import com.hobart.learning.lagou.edu.dao.IAccountDao;
import com.hobart.learning.lagou.edu.pojo.Account;
import com.hobart.learning.lagou.edu.utils.ConnectionUtils;
import com.hobart.learning.lagou.edu.utils.DruidUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author ：hobart
 * @date ：Created in 2021-1-28 16:31
 * @description： 转账 DAO
 * @version: 1.0
 */
@Repository
public class AcountDaoImpl implements IAccountDao {

    // 第一版

//    @Override
//    public Account queryAccountByCardNo(String cardNo) throws Exception {
//        //从连接池获取连接
//        Connection con = DruidUtils.getInstance().getConnection();
//        String sql = "select * from account where card_no=?";
//        PreparedStatement preparedStatement = con.prepareStatement(sql);
//        preparedStatement.setString(1, cardNo);
//        ResultSet resultSet = preparedStatement.executeQuery();
//
//        Account account = new Account();
//        while (resultSet.next()) {
//            account.setCardNo(resultSet.getString("card_no"));
//            account.setName(resultSet.getString("name"));
//            account.setMoney(resultSet.getInt("money"));
//        }
//
//        resultSet.close();
//        preparedStatement.close();
//
//        return account;
//    }
//
//    @Override
//    public int updateAccountByCardNo(Account account) throws Exception {
//        // 从连接池获取连接
//        // 改造为：从当前线程当中获取绑定的connection连接
//        Connection con = DruidUtils.getInstance().getConnection();
//        String sql = "update account set money=? where card_no=?";
//        PreparedStatement preparedStatement = con.prepareStatement(sql);
//        preparedStatement.setInt(1, account.getMoney());
//        preparedStatement.setString(2, account.getCardNo());
//        int i = preparedStatement.executeUpdate();
//
//        preparedStatement.close();
//        //con.close();
//        return i;
//    }

    // 第二版
//    @Override
//    public Account queryAccountByCardNo(String cardNo) throws Exception {
//        //从连接池获取连接
//        Connection con = ConnectionUtils.getInstance().getCurrentThreadConn();
//        String sql = "select * from account where card_no=?";
//        PreparedStatement preparedStatement = con.prepareStatement(sql);
//        preparedStatement.setString(1, cardNo);
//        ResultSet resultSet = preparedStatement.executeQuery();
//
//        Account account = new Account();
//        while (resultSet.next()) {
//            account.setCardNo(resultSet.getString("card_no"));
//            account.setName(resultSet.getString("name"));
//            account.setMoney(resultSet.getInt("money"));
//        }
//
//        resultSet.close();
//        preparedStatement.close();
//
//        return account;
//    }
//
//    @Override
//    public int updateAccountByCardNo(Account account) throws Exception {
//        // 从连接池获取连接
//        // 改造为：从当前线程当中获取绑定的connection连接
//        Connection con = ConnectionUtils.getInstance().getCurrentThreadConn();
//        String sql = "update account set money=? where card_no=?";
//        PreparedStatement preparedStatement = con.prepareStatement(sql);
//        preparedStatement.setInt(1, account.getMoney());
//        preparedStatement.setString(2, account.getCardNo());
//        int i = preparedStatement.executeUpdate();
//
//        preparedStatement.close();
//        //con.close();
//        return i;
//    }

    // 第三版
//    private ConnectionUtils connectionUtils;
//
//    public void setConnectionUtils(ConnectionUtils connectionUtils) {
//        this.connectionUtils = connectionUtils;
//    }

    @Autowired
    private ConnectionUtils connectionUtils;

    @Override
    public Account queryAccountByCardNo(String cardNo) throws Exception {
        //从连接池获取连接
        Connection con = connectionUtils.getCurrentThreadConn();
        String sql = "select * from account where card_no=?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, cardNo);
        ResultSet resultSet = preparedStatement.executeQuery();

        Account account = new Account();
        while (resultSet.next()) {
            account.setCardNo(resultSet.getString("card_no"));
            account.setName(resultSet.getString("name"));
            account.setMoney(resultSet.getInt("money"));
        }

        resultSet.close();
        preparedStatement.close();

        return account;
    }

    @Override
    public int updateAccountByCardNo(Account account) throws Exception {
        // 从连接池获取连接
        // 改造为：从当前线程当中获取绑定的connection连接
        Connection con = connectionUtils.getCurrentThreadConn();
        String sql = "update account set money=? where card_no=?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setInt(1, account.getMoney());
        preparedStatement.setString(2, account.getCardNo());
        int i = preparedStatement.executeUpdate();

        preparedStatement.close();
        //con.close();
        return i;
    }
}